<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/3
 * Time: 9:57
 */
namespace app\index\controller;

use think\Db;
use think\Session;
use think\View;
use think\Controller;
use app\index\model;

class Guquan  extends Controller
{
    public function index()
    {
        $id=getProviderId();
        //省
        $province=Db::name('area')->where('level',0)->select();;
//        var_dump($province);die;
        $this->view->assign('province',$province);
        //市
        $city=Db::name('area')->where('level',1)->select();
        $this->view->assign('city',$city);
        $area=Db::name('area')->where('level',2)->select();
        $this->view->assign('area',$area);
        $data=Db::name('project')->where('status',1)->where(['projectType'=>3,'isSelled'=>0])->join('project_tradelicense','project.tradeLicenseId=project_tradelicense.tradeLicenseId')->field('projectId,projectName,project.tradeLicenseId,establishmentDateTime,registerCapital,capitalCurrency,taxType,price,cityId,districtId,memberId')->order("case when memberId=".$id." then 1 ELSE 4 END", null)->limit(10)->select();
        foreach ($data as $k=> $v){
            //公司地址
            $cityName=Db::name('area')->where('areaId',$v['cityId'])->value('areaName');
            $district=Db::name('area')->where('areaId',$v['districtId'])->value('areaName');
            $data[$k]['areaName']=$cityName.$district;
            //公司税务类型
            $taxType=Db::name('menu')->join('option','menu.menuId=option.menuId')->where('menuCode','taxt')->where('optionCode',$v['taxType'])->field('optionName')->select();
            $tax=array();
            foreach ($taxType as $v2){
                $tax[]=$v2['optionName'];
            }
            $tType=implode(' ',$tax);
            $data[$k]['taxType']=$tType;
        }
        $this->view->assign('data',$data);
        return view('guquanlist');
    }
    public function getCity()
    {
        $city=Db::name('area')->where('parentId',input('province'))->select();
        foreach ($city as $k =>$v){
            $city[$k]['area']=Db::name('area')->where('parentId',$v['areaId'])->select();
        }
        return $city;
    }
    public function getArea()
    {
        return Db::name('area')->where('parentId',input('city'))->select();
    }
    public function search()
    {
        $id=getProviderId();
        if(input('location')!="") {
            $where['districtId'] = input('location');
        }else{
            $where="";
        }
//        var_dump($where);die;
        if(input('stamp')!=""){
            $whe=$_POST['stamp'];
        }else{
            $whe="";
        }
        if(input('search')!=""){
            $whe=input('search');
        }
        $data=Db::name('project')->where($where)->where('status',1)->where(['projectType'=>3,'isSelled'=>0])->join('project_tradelicense','project.tradeLicenseId=project_tradelicense.tradeLicenseId')->whereLike('projectName','%'.$whe.'%')->field('projectId,projectName,project.tradeLicenseId,establishmentDateTime,registerCapital,capitalCurrency,taxType,price,cityId,districtId,memberId')->order("case when memberId=".$id." then 1 ELSE 4 END", null)->limit(10)->select();
        foreach ($data as $k=> $v){
            //公司地址
            $cityName=Db::name('area')->where('areaId',$v['cityId'])->value('areaName');
            $district=Db::name('area')->where('areaId',$v['districtId'])->value('areaName');
            $data[$k]['areaName']=$cityName.$district;
            //公司税务类型
            $taxType=Db::name('menu')->join('option','menu.menuId=option.menuId')->where('menuCode','taxt')->where('optionCode',$v['taxType'])->field('optionName')->select();
            $tax=array();
            foreach ($taxType as $v2){
                $tax[]=$v2['optionName'];
            }
            $tType=implode(' ',$tax);
            $data[$k]['taxType']=$tType;
        }
        return $data;

    }
    public function detail()
    {
        $id=$_GET['projectId'];
        $project=Db::name('project')->where('projectId',$id)->join('project_tradelicense','project.tradeLicenseId=project_tradelicense.tradeLicenseId')->field('projectId,projectName,project.tradeLicenseId,establishmentDateTime,registerCapital,taxType,price,cityId,districtId,companyName,licenseType,businessScope,creditNo,registerAddress,taxSituation,bankAccount,memberId,accountIsNormal,addDateTime,capitalCurrency,memberId,tradeFileId,isSelled')->find();
        $cityName=Db::name('area')->where('areaId',$project['cityId'])->value('areaName');
        $district=Db::name('area')->where('areaId',$project['districtId'])->value('areaName');
        $project['areaName']=$cityName.$district;
//        var_dump($project);die;
        //发布人 姓名 电话
        if($project['memberId']==0){
            $provider=getPlatform();
            $member['memberName']=$provider['providerName'];
            $member['mobile']=$provider['mobile'];
        }else{
            $member=Db::name('member')->where('memberId',$project['memberId'])->field('memberName,mobile')->find();
        }

        $project['memberName']=$member['memberName'];
        $project['mobile']=$member['mobile'];

        //公司类型
        $license=Db::name('menu')->join('option','menu.menuId=option.menuId')->where('menuCode','ct')->where('optionCode',$project['licenseType'])->value('optionName');
        $project['license']=$license;
        //注册资本币种
        $capitalCurrency=Db::name('menu')->join('option','menu.menuId=option.menuId')->where('menuCode','cac')->where('optionCode',$project['capitalCurrency'])->value('optionName');
        $project['capitalCurrency']=$capitalCurrency;
        //公司税务类型
        $taxType=Db::name('menu')->join('option','menu.menuId=option.menuId')->where('menuCode','taxt')->where('optionCode',$project['taxType'])->value('optionName');
        $project['taxType']=$taxType;
        //报税情况
        $tax=Db::name('menu')->join('option','menu.menuId=option.menuId')->where('menuCode','taxs')->where('optionCode',$project['taxSituation'])->value('optionName');
        $project['taxSituation']=$tax;
        //银行账户
        $bankAccount=Db::name('menu')->join('option','menu.menuId=option.menuId')->where('menuCode','ba')->where('optionCode',$project['bankAccount'])->value('optionName');
        $project['bankAccount']=$bankAccount;
        //银行账户是否正常
        $accountIsNormal=Db::name('menu')->join('option','menu.menuId=option.menuId')->where('menuCode','yn')->where('optionCode',$project['accountIsNormal'])->value('optionName');
        $project['accountIsNormal']=$accountIsNormal;

        //获取session信息
        $memberId=Session('Login_userId');
        $this->view->assign('memberId', $memberId);
        //查看次数  用户余额
        $num=Db::name('member_account')->where('memberId',$memberId)->field('visibleNum,account')->find();
        $this->view->assign('num', $num);
        $paymentStatus=Db::name('service_order')->join('service_order_service','service_order.serviceOrderId=service_order_service.serviceOrderId')->where(['memberId'=>$memberId,'serviceId'=>$project['projectId']])->value('paymentStatus');
        if($paymentStatus==1){
            //项目图片
            $files=Db::name('files')->where('filesId',$project['tradeFileId'])->value('thumbFile');
            $project['files']=$files;
        }else{
            $project['files']="__STATIC__/images/guquan2.png";
        }
        $this->view->assign('project', $project);
        return view('guquaninfo');
    }
    public function append()
    {
        if(input('location')!="") {
            if(input('location')=='undefined'){
                $where="";
            }else{
                $where['districtId'] = input('location');
            }
        }else{
            $where="";
        }
//        var_dump($where);die;
        if(input('stamp')!=""){
            $whe=$_POST['stamp'];
        }else{
            $whe="";
        }
        if(input('search')!=""){
            $whe=input('search');
        }
        $start=input('num')*10;
        $end=$start+10;
        $id=getProviderId();
        $data=Db::name('project')->where($where)->where('status',1)->where(['projectType'=>3,'isSelled'=>0])->join('project_tradelicense','project.tradeLicenseId=project_tradelicense.tradeLicenseId')->whereLike('projectName','%'.$whe.'%')->field('projectId,projectName,project.tradeLicenseId,establishmentDateTime,registerCapital,capitalCurrency,taxType,price,cityId,districtId,memberId')->order("case when memberId=".$id." then 1 ELSE 4 END", null)->limit($start,$end)->select();
        foreach ($data as $k=> $v){
            //公司地址
            $cityName=Db::name('area')->where('areaId',$v['cityId'])->value('areaName');
            $district=Db::name('area')->where('areaId',$v['districtId'])->value('areaName');
            $data[$k]['areaName']=$cityName.$district;
            //公司税务类型
            $taxType=Db::name('menu')->join('option','menu.menuId=option.menuId')->where('menuCode','taxt')->where('optionCode',$v['taxType'])->field('optionName')->select();
            $tax=array();
            foreach ($taxType as $v2){
                $tax[]=$v2['optionName'];
            }
            $tType=implode(' ',$tax);
            $data[$k]['taxType']=$tType;
        }
        return $data;
    }
}